/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui;

import java.util.ArrayList;
import java.util.List;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.classes.Course;
import utsc.apps.utschedule.classes.GenericTask;
import utsc.apps.utschedule.ui.activities.adapters.CourseListAdapter;
import utsc.apps.utschedule.util.database.DatabaseManager;

import android.graphics.Color;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockListFragment;

/**
 * This is a list fragment to display a list of {@link Course archive courses} of a given timetable.
 *
 */
public class CourseArchiveListFragment extends SherlockListFragment {
	
	private int _timetableID;
	private static final String TIMETABLE_KEY="TimetableID";
	private CourseListAdapter _courseListAdapter;
	
	/**
	 * Setup a new instance by passing the timetable id as an argument.
	 * @param timetableID the given timetable's timetable id
	 * @return a new instance with the timetable id as argument
	 */
	public static CourseArchiveListFragment newInstance(int timetableID){
		CourseArchiveListFragment fragment =new CourseArchiveListFragment();
		Bundle args=new Bundle();
		args.putInt(TIMETABLE_KEY, timetableID);
		fragment.setArguments(args);
		return fragment;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//on create, retrieve the arguments
		Bundle args=getArguments();
		if (args!=null) {
			//try to retrieve the timetable id from the arguments
			_timetableID=args.getInt(TIMETABLE_KEY);
		}
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//set the cache color hint to transparent
		getListView().setCacheColorHint(Color.TRANSPARENT);
		//set the conent background to light gray
		getView().setBackgroundColor(Color.LTGRAY);
		//set the empty text as well
		setEmptyText("No Archive Courses found!");
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//only setup the fragment if the timetable id is valid
		if (_timetableID!=0) {
			DatabaseManager dbManager=DatabaseManager.getInstance(getSherlockActivity());
			//get the list of courses with the given timetable id
			List<Course> courses=dbManager.getCoursesWithoutTutorials(_timetableID);
			if (courses!=null && !courses.isEmpty()) {
				//iterate course list and setup the task list
				for (Course course : courses) {
					List<GenericTask> tasks=dbManager.getCourseTasks(course.get_courseID());
					course.set_taskList(new ArrayList<GenericTask>(tasks));
				}
			}else {
				courses=new ArrayList<Course>();
			}
			//setup the list adatper
			_courseListAdapter=new CourseListAdapter(getSherlockActivity(), R.layout.course_list_item, courses);
			setListAdapter(_courseListAdapter);
		}
	}
}
